//
//  XJYLogin.m
//  高仿萝卜书摘
//
//  Created by 谢俊逸 on 2016/11/27.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//

#import "XJYLogin.h"
#import "AppDelegate.h"
#import "XJYHeader.h"


static User *curLoginUser;

@implementation XJYLogin
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.remember_me = [NSNumber numberWithBool:YES];
        self.name = @"";
        self.email = @"";
        self.password = @"";
    }
    return self;
}


- (NSString *)toPath{
    return @"api/vi/account/login";
}

- (NSDictionary *)toParams{
    NSDictionary *params = @{@"name": self.name,
                                    @"password" : self.password,
                                    @"email": self.email
                                    };

    return params;
}
+ (BOOL)isLogin{
    NSNumber *loginStatus = [[NSUserDefaults standardUserDefaults]objectForKey:kLoginStatus];
    if (loginStatus.boolValue && [XJYLogin curLoginUser]) {
        User *loginUser = [XJYLogin curLoginUser];
        if (loginUser.status && loginUser.status.integerValue == 0) {
            return NO;
        }
        return YES;
    }else{
        return NO;
    }
    
}

+ (NSString *)loginDataListPath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return [documentPath stringByAppendingPathComponent:kLoginDataListPath];
}

+ (NSMutableDictionary *)readLoginDataList{
    NSMutableDictionary *loginDataList = [NSMutableDictionary dictionaryWithContentsOfFile:[self loginDataListPath]];
    if(!loginDataList){
        //Creates and returns an empty dictionary.
        loginDataList = [NSMutableDictionary dictionary];
    }
    return loginDataList;
}

+ (BOOL)saveLoginData:(NSDictionary *)loginData{
    BOOL saved = NO;
    if(loginData){
        User *curUser = [User mj_objectWithKeyValues:loginData];
        if (curUser.name&&curUser.password) {
            saved = YES;
        }
        if (saved) {
            saved = [loginData writeToFile:[self loginDataListPath] atomically:YES];
        }
    }
    
    return saved;
}

+ (void)doLogin:(NSDictionary *)loginData{
    if(loginData){
        NSLog(@"LoginData = %@",loginData);
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[NSNumber numberWithBool:YES] forKey:kLoginStatus];
        [defaults setObject:loginData forKey:kLoginUserDict];
        [defaults synchronize];
        [self saveLoginData:loginData];
    }else{
        [self doLogout];
    }
}


+ (void)doLogout{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:NO] forKey:kLoginStatus];
    [defaults synchronize];
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    [cookies enumerateObjectsUsingBlock:^(NSHTTPCookie *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.domain hasSuffix:@".coding.net"]) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:obj];
        }
    }];
    
}
+ (User *)curLoginUser{
    if(!curLoginUser){
        NSDictionary *loginData = [[NSUserDefaults standardUserDefaults]objectForKey:kLoginUserDict];
        curLoginUser = loginData?[User mj_objectWithKeyValues:loginData]:nil;
    }
    return curLoginUser;
}

+ (void)setPreUserEmail:(NSString *)emailStr{
    if (emailStr.length <= 0) {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:emailStr forKey:kLoginPreUserEmail];
    [defaults synchronize];
}

+ (NSString *)preUserEmail{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:kLoginPreUserEmail];
}

@end
